package com.autumn.platform.dao.schema.function.impl;

import org.apache.ibatis.session.Configuration;
import org.springframework.core.Ordered;

import com.autumn.platform.core.support.order.impl.Orderable;
import com.autumn.platform.dao.schema.function.ISqlConfigFunction;

public abstract class AbstractSqlConfigFunction extends Orderable implements ISqlConfigFunction {

    public AbstractSqlConfigFunction() {
        super(Ordered.LOWEST_PRECEDENCE);
    }

    abstract protected String getName();

    protected boolean supports(Configuration configuration) {
        return true;
    }

    @Override
    public boolean supports(String name, Configuration configuration) {
        return name.equals(getName());
    }

    protected String join(String[] arr, String separator) {
        if (null == arr || arr.length == 0) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (Object obj : arr) {
            if (null != obj) {
                sb.append(separator).append(obj);
            }
        }
        return sb.substring(separator.length()).toString();
    }

    protected void assertEqualArgsCount(String[] args, int count) {
        if (null != args && args.length != count) {
            throw new IllegalArgumentException("the count of sql-config-function [" + getName() + "] args must be " + count + ", but actual is " + args.length);
        }
    }

    protected void assertAtLeastArgsCount(String[] args, int count) {
        if (null != args && args.length < count) {
            throw new IllegalArgumentException("the count of sql-config-function [" + getName() + "] args at least is " + count + ", but actual is " + args.length);
        }
    }
}
